package main

import (
	"fmt"
	"time"
)

func findAnagrams(s string, p string) []int {
	result := []int{}
	count := make(map[byte]int)
	temp := make(map[byte]int)
	flag := false
	// sliceS = []string{s}
	// sliceP = []string{p}
	// 把p转化为字典
	if len(s) < len(p) {
		return result
	}

	for i := 0; i < len(p); i++ {
		count[p[i]]++
	}
	// fmt.Println(count, len(p))
	for j := 0; j < len(p); j++ {
		temp[s[j]]++
	}
	// fmt.Println(temp)

	for i := 0; i < len(s)-len(p)+1; i++ {
		if i > 0 {
			temp[s[i-1]]--
			temp[s[i+len(p)-1]]++
		}
		// fmt.Println(temp)

		for k, _ := range count {
			// fmt.Println(k, v)
			if temp[k] == count[k] {
				flag = true
			} else {
				flag = false
				break
			}
		}
		if flag == true {
			// fmt.Println(temp)
			flag = false
			result = append(result, i)
		}
	}
	return result

}

func main() {
	start := time.Now()
	s := "slfjbssdsbsssljf"
	p := "sb"
	result := findAnagrams(s, p)
	cost := time.Since(start)
	fmt.Println("result:", result)
	fmt.Println(cost)
}
